home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / base / netkit-b.07a / netkit-b / NetKit-B-0.07A / talk / get_addrs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-20  |  3.8 KB  |  115 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /*
  35.  * From: @(#)get_addrs.c    5.7 (Berkeley) 3/1/91
  36.  */
  37. char ga_rcsid[] = 
  38.   "$Id: get_addrs.c,v 1.4 1996/07/20 20:59:41 dholland Exp $";
  39.  
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <net/if.h>
  43. #include <netinet/in.h>
  44. #include <protocols/talkd.h>
  45. #include <netdb.h>
  46. #include <stdio.h>
  47. #include <unistd.h>
  48. #include "talk.h"
  49. #include "talk_ctl.h"
  50.  
  51. void
  52. get_addrs(char *my_machine_name, char *his_machine_name)
  53. {
  54.     struct hostent *hp;
  55.     struct servent *sp;
  56.  
  57.     msg.pid = htonl(getpid());
  58.     /* look up the address of the local host */
  59.     hp = gethostbyname(my_machine_name);
  60.     if (hp == NULL) {
  61.         fprintf(stderr, "talk: %s: ", my_machine_name);
  62.         herror((char *)NULL);
  63.         exit(-1);
  64.     }
  65.     memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
  66.     /*
  67.      * If the callee is on-machine, just copy the
  68.      * network address, otherwise do a lookup...
  69.      */
  70.     if (strcmp(his_machine_name, my_machine_name)) {
  71.         hp = gethostbyname(his_machine_name);
  72.         if (hp == NULL) {
  73.             fprintf(stderr, "talk: %s: ", his_machine_name);
  74.             herror((char *)NULL);
  75.             exit(-1);
  76.         }
  77.         memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
  78.     } else
  79.         his_machine_addr = my_machine_addr;
  80.     /* find the server's port */
  81.     sp = getservbyname("ntalk", "udp");
  82.     if (sp == 0) {
  83.         fprintf(stderr, "talk: %s/%s: service is not registered.\n",
  84.              "ntalk", "udp");
  85.         exit(-1);
  86.     }
  87.     daemon_port = sp->s_port;
  88. #ifdef    BAD_LINUX_HACK
  89.     /* now some hacking to get my own address correctly routed */
  90.     {
  91.         FILE *fp;
  92.         long dest, mask;
  93.         char line[512];
  94.         struct    ifreq ifr;
  95.         int    s;
  96.  
  97.         /* locate the proper device it has to go to */
  98.         fp = fopen("/proc/net/route","r");
  99.         fgets(line,sizeof(line),fp);
  100.         while (fscanf(fp,"%s %lx %*lx %*d %*d %*d %*d %lx %*d %*d\n",ifr.ifr_name,&dest,&mask)==3) {
  101.             if ((his_machine_addr.s_addr & mask) == dest) {
  102.                 /* ok, now find out its addr, and use that as
  103.                    OUR address */
  104.                 s = socket(AF_INET,SOCK_DGRAM,0);
  105.                 ioctl(s,SIOCGIFADDR,&ifr);
  106.                 close(s);                
  107.                 my_machine_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  108.                 break;
  109.             }
  110.         }
  111.         fclose(fp);
  112.     }
  113. #endif
  114. }
  115.